/*
 * 退货通知单pdf controller
 */
public with sharing class ReturnOrderPDFController
{
    public String roId ;//退货订单id
    public List<ReturnRequisitionListBean> rodList {get;set;}//tu退货明细
    public ReturnRequisition__c rre{get;set;}//退货通知单
    public String isInvoice {get;set;}//是否有发票
    public String returnday {get;set;}//退货日期
    
    public ReturnOrderPDFController()
    {
          roId = System.currentPageReference().getParameters().get('id');
          rodList = new List<ReturnRequisitionListBean> ();
          rre = new ReturnRequisition__c();
        init();
    }
    public void init(){
        
       rre = [select Id,CreatedBy.Name,Company__c,Account__r.Name ,Invoice__c,InvoiceNumber__c,ReturnDay__c,Name from ReturnRequisition__c where Id =:roId];
       isInvoice = rre.Invoice__c == true ? '是':'否';
       returnday = rre.ReturnDay__c ==  null ? '':rre.ReturnDay__c.format();
       Integer i = 0;
        // ExpirationDate__c BatchNumber__c
       for(ReturnRequisitionList__c r: [select SerialNo__c,Brand__c,Spec__c,Model__c,NCvpzwh__c,Product__r.Name,
                                        Unit__c,RefundNumber__c,NCNumber__c,BatchNumber__c,ExpirationDate__c,
                                        Note__c from ReturnRequisitionList__c where ReturnRequisition__c =:roId])
       {
            i++;
            Integer len = 0;
            if(r.Product__r.Name != null)len = r.Product__r.Name.length();
            if(r.Brand__c != null && len < r.Brand__c.length())len = r.Brand__c.length();
            if(r.Spec__c != null && len < r.Spec__c.length())len = r.Spec__c.length();
            if(r.Model__c != null && len < r.Model__c.length())len = r.Model__c.length();
            if(r.NCvpzwh__c != null && len < r.NCvpzwh__c.length())len = r.NCvpzwh__c.length();
            if(r.Note__c != null && len < r.Note__c.length())len = r.Note__c.length();

            Decimal rowheight = len <= 8 ? 34.0 :(len/ 8== 1 ? 34.0 : (len/8  *12 + 10));
            rodList.add(new ReturnRequisitionListBean(r,i,rowheight));

           
       }
       if(i < 6){
       for(Integer j =6 ; j > i;j--){
                rodList.add(new ReturnRequisitionListBean());
           }
       }
       
       
    }
   
    public class ReturnRequisitionListBean {
        public Integer rownum {get;set;}//行号
        public String  invname {get;set;}//存货名称
        public String  serialno {get;set;}//存货代码
        public String  brand {get;set;}//品牌
        public String  spec {get;set;}//规格
        public String  model {get;set;}//型号
        public String ncvpzwh {get;set;}//批准文号
        public String  unit {get;set;}//单位
        public Decimal refundnumber {get;set;}//退货数量
        public String  batchnumber {get;set;}//批号
        public String expiredate {get;set;}//有效期
        public String  ncnumber {get;set;}//单据号
        public String note {get;set;}//备注
      	public  Decimal rowheight {get;set;}
        public ReturnRequisitionListBean(){
        rowheight = 34;
        }
        public ReturnRequisitionListBean(ReturnRequisitionList__c rlist,Integer row,Decimal rh){
            
        rownum = row;
            invname = rlist.Product__r.Name == null ? '':rlist.Product__r.Name.length()>8 ?autoNextLine(rlist.Product__r.Name,8):rlist.Product__r.Name;
            serialno = rlist.SerialNo__c ;
            brand = rlist.Brand__c == null ?'':rlist.Brand__c.length()>8?autoNextLine(rlist.Brand__c,8):rlist.Brand__c;
            spec = rlist.Spec__c== null ?'':rlist.Spec__c.length()>8?autoNextLine(rlist.Spec__c,8):rlist.Spec__c;
            model = rlist.Model__c== null ?'':rlist.Model__c.length()>8?autoNextLine(rlist.Model__c,8):rlist.Model__c;
            ncvpzwh = rlist.NCvpzwh__c== null ?'':rlist.NCvpzwh__c.length()>8?autoNextLine(rlist.NCvpzwh__c,8):rlist.NCvpzwh__c;
            unit = rlist.Unit__c;
            refundnumber = rlist.RefundNumber__c;
            batchnumber = rlist.BatchNumber__c;
            expiredate = rlist.ExpirationDate__c == null ? '':rlist.ExpirationDate__c.format();
           // ncnumber = rlist.NCNumber__c ==null ?'':rlist.NCNumber__c.length()>8?autoNextLine(rlist.NCNumber__c,8):rlist.NCNumber__c;
            note = rlist.Note__c== null ?'':rlist.Note__c.length()>8?autoNextLine(rlist.Note__c,8):rlist.Note__c;
           
            rowheight = rh;
            
            /*rownum = row;
            invname = rlist.Invname__c;
            serialno = rlist.SerialNo__c ;
            brand = rlist.Brand__c;
            spec = rlist.Spec__c;
            model = rlist.Model__c;
            ncvpzwh = rlist.NCvpzwh__c;
            unit = rlist.Unit__c;
            refundnumber = rlist.RefundNumber__c;
            batchnumber = rlist.BatchNumber__c;
            expiredate = rlist.ExpirationDate__c == null ? '':rlist.ExpirationDate__c.format();
            ncnumber = rlist.NCNumber__c;
            note = rlist.Note__c;*/
           
            //rowheight = 42;
            
            
        }
        
         private String autoNextLine(String s, Integer posLength ){
          if(s == null || s.length() <=posLength ) 
          {
              return s; 
          }
           String temp = '';
                  if(s.length() > posLength){
                     for(Integer i = 0; i < s.length(); i++){
                     if(Math.mod(i, posLength ) == 0){
                           if(s.length() > i + posLength){
                             temp += s.substring(i, i + posLength) + '\n'; 
                         }else{ 
                             temp+= s.substring(i,s.length());
                          } 
                  }
                     }
                  }

              return temp;
    }
    }
}